package com.lanhe.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.core.domain.entity.LhMemberBase;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.XmlUtils;
import com.lanhe.common.utils.wechat.WxPayUtil;
import com.lanhe.common.utils.wechat.vo.PrePayNotifyDataVo;
import com.lanhe.common.utils.wechat.vo.PrePayVo;
import com.lanhe.common.utils.zhifubao.ZhiFuBaoUtil;
import com.lanhe.common.utils.zhifubao.vo.ZhiFuBaoPayNotifyDataVo;
import com.lanhe.common.utils.zhifubao.vo.ZhiFuBaoVo;
import com.lanhe.member.domain.LhMemberConsumedRec;
import com.lanhe.member.service.ILhMemberBaseService;
import com.lanhe.member.service.ILhMemberConsumedRecService;
import com.lanhe.order.domain.LhOrdersLevel;
import com.lanhe.order.domain.LhOrdersLevelLog;
import com.lanhe.order.mapper.LhOrdersLevelMapper;
import com.lanhe.order.service.ILhOrdersLevelLogService;
import com.lanhe.order.service.ILhOrdersLevelService;
import com.lanhe.product.domain.LhLevelInfo;
import com.lanhe.product.mapper.LhLevelInfoMapper;
import com.lanhe.product.service.ILhLevelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 会员订单表(LhOrdersLevel)表服务实现类
 *
 * @author zhanggl
 * @since 2024-01-04 22:27:25
 */
@Service
public class LhOrdersLevelServiceImpl extends ServiceImpl<LhOrdersLevelMapper, LhOrdersLevel> implements ILhOrdersLevelService {

	@Autowired
	private LhOrdersLevelMapper lhOrdersLevelMapper;
	@Autowired
	private ILhOrdersLevelLogService ordersLevelLogService;

	@Autowired
	private ILhLevelInfoService lhLevelInfoService;

	@Autowired
	private ILhMemberBaseService lhMemberBaseService;

	@Autowired
	private WxPayUtil wxPayUtil;

	@Autowired
	private ILhMemberConsumedRecService memberConsumedRecService;

	/**
	 * VIP购买
	 *
	 * @param level 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/4
	 */
	@Override
	public PrePayVo insertOrdersLevel(LhOrdersLevel level) {

		// 判断产品是否存在
		LhLevelInfo lhLevelInfo = lhLevelInfoService.getById(level.getLevelId());
		if (ObjectUtil.isNull(lhLevelInfo)) {
			throw new ServiceException("购买产品不存在", HttpStatus.ERROR);
		}

		// 判断用户是否已经购买
		LhMemberBase memberBase = lhMemberBaseService.getById(level.getMemberId());
		if (ObjectUtil.isNull(memberBase)) {
			throw new ServiceException("用户不存在", HttpStatus.ERROR);
		}

		// VIP删除后，不可以购买
		if (Constants.GeneralIntType.TWO.toString().equals(lhLevelInfo.getDelFlag())){
			throw new ServiceException("VIP已下架,不支持购买", HttpStatus.ERROR);
		}

		Integer price = lhLevelInfo.getPrice();
		if (Constants.ORDER_TYPE_1.equals(level.getOrderType())) {

			String levelShow = lhLevelInfo.getLevelShow();
			String[] levelShowArr = levelShow.split(",");

			// 将字符串数组转换为整数数组
			int[] intArray = Arrays.stream(levelShowArr)
					.mapToInt(Integer::parseInt)
					.toArray();

			// 判断是否符合购买条件
			if (!Arrays.stream(intArray).anyMatch(num -> num == memberBase.getLeaderLevel())) {
				throw new ServiceException("不符合购买条件", HttpStatus.ERROR);
			}


		} else {

			if (!lhLevelInfo.getLeaderLevel().equals(memberBase.getLeaderLevel())) {
				throw new ServiceException("不符合续费条件", HttpStatus.ERROR);
			}

			price = lhLevelInfo.getRenewPrice();
			// 续费如果需要积分，则处理积分逻辑
			if (lhLevelInfo.getRenewScore() > 0) {
				if (memberBase.getResidueScore() < lhLevelInfo.getRenewScore()) {
					throw new ServiceException("积分不足，不能进行续费", HttpStatus.ERROR);
				}

				// 减少积分
				lhMemberBaseService.reduceResidueScore(memberBase.getMemberId(), lhLevelInfo.getRenewScore());
			}
		}

		// 生成订单号
		String outTradeNo = wxPayUtil.getOutTradeNo();

		// 生成随机字符串
		String nonceStr = wxPayUtil.getNonceStr();

		// 生产订单
		level.setOrderNumber(outTradeNo);
		level.setOrderType(level.getOrderType());
		level.setPurchaseType(lhLevelInfo.getPurchaseType());
		level.setTitle(lhLevelInfo.getTitle());
		level.setPrice(price);
		level.setPlatform(Constants.PLATFORM_2);
		level.setPayed(Constants.PAYED_NO);
		level.setCreateTime(new Date());
		lhOrdersLevelMapper.insert(level);

		// 获取当前系统时间戳
		Timestamp sysTime = new Timestamp(System.currentTimeMillis());
		long timestamp = sysTime.getTime();

		// 调用微信统一下单接口
		String prePayMsg = null;
		try {
			prePayMsg = wxPayUtil.prePay(String.valueOf(price * 100), lhLevelInfo.getTitle() + "购买", nonceStr, outTradeNo);
		} catch (Exception e) {
			throw new ServiceException("第一次签名失败", HttpStatus.ERROR);
		}

		// 获得prePayId
		String prePayId = null;
		try {
			if (StringUtils.isBlank(prePayMsg)) {
				throw new ServiceException("第一次签名返回失败", HttpStatus.ERROR);
			}
			prePayId = prePayMsg.substring(prePayMsg.indexOf("<prepay_id><![CDATA[") + 20, prePayMsg.indexOf("]]></prepay_id>"));
		} catch (Exception e) {
			throw new ServiceException("生成预订单失败", HttpStatus.ERROR);
		}

		// 对数据进行二次签名
		return wxPayUtil.secondsSign(prePayId, nonceStr, timestamp);
	}

	/**
	 * 自定义支付
	 * @param level
	 * @return
	 */
	@Override
	public PrePayVo insertZidingyiOrdersLevel(LhOrdersLevel level) {

		LhLevelInfo lhLevelInfo = lhLevelInfoService.selectLevelInfoById(7L);
		if (ObjectUtil.isNull(lhLevelInfo)) {
			throw new ServiceException("购买产品不存在", HttpStatus.ERROR);
		}

		// VIP删除后，不可以购买
		if (Constants.GeneralIntType.TWO.toString().equals(lhLevelInfo.getDelFlag())){
			throw new ServiceException("VIP已下架,不支持购买", HttpStatus.ERROR);
		}

		// 生成订单号
		String outTradeNo = "ZDY_"+System.currentTimeMillis();

		// 生成随机字符串
		String nonceStr = wxPayUtil.getNonceStr();

		// 生产订单
		level.setLevelId(7L);
		level.setOrderNumber(outTradeNo);
		level.setOrderType(level.getOrderType());
		level.setPurchaseType(1);
		level.setTitle(lhLevelInfo.getTitle());
		level.setPrice(level.getPrice());
		level.setPlatform(Constants.PLATFORM_2);
		level.setPayed(Constants.PAYED_NO);
		level.setCreateTime(new Date());
		lhOrdersLevelMapper.insert(level);

		// 获取当前系统时间戳
		Timestamp sysTime = new Timestamp(System.currentTimeMillis());
		long timestamp = sysTime.getTime();

		// 调用微信统一下单接口
		String prePayMsg = null;
		try {
			prePayMsg = wxPayUtil.prePay(String.valueOf(level.getPrice() * 100), lhLevelInfo.getTitle() + "购买", nonceStr, outTradeNo);
		} catch (Exception e) {
			throw new ServiceException("第一次签名失败", HttpStatus.ERROR);
		}

		// 获得prePayId
		String prePayId = null;
		try {
			if (StringUtils.isBlank(prePayMsg)) {
				throw new ServiceException("第一次签名返回失败", HttpStatus.ERROR);
			}
			prePayId = prePayMsg.substring(prePayMsg.indexOf("<prepay_id><![CDATA[") + 20, prePayMsg.indexOf("]]></prepay_id>"));
		} catch (Exception e) {
			throw new ServiceException("生成预订单失败", HttpStatus.ERROR);
		}

		// 对数据进行二次签名
		return wxPayUtil.secondsSign(prePayId, nonceStr, timestamp);
	}


	/**
	 * 支付宝购买
	 *
	 * @param level 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/4
	 */
	@Override
	public String insertOrdersLevelPayALi(LhOrdersLevel level) {

		// 判断产品是否存在
		LhLevelInfo lhLevelInfo = lhLevelInfoService.getById(level.getLevelId());
		if (ObjectUtil.isNull(lhLevelInfo)) {
			throw new ServiceException("购买产品不存在", HttpStatus.ERROR);
		}

		// 判断用户是否已经购买
		LhMemberBase memberBase = lhMemberBaseService.getById(level.getMemberId());
		if (ObjectUtil.isNull(memberBase)) {
			throw new ServiceException("用户不存在", HttpStatus.ERROR);
		}

		// VIP删除后，不可以购买
		if (Constants.GeneralIntType.TWO.toString().equals(lhLevelInfo.getDelFlag())){
			throw new ServiceException("VIP已下架,不支持购买", HttpStatus.ERROR);
		}

		Integer price = lhLevelInfo.getPrice();
		if (Constants.ORDER_TYPE_1.equals(level.getOrderType())) {

			String levelShow = lhLevelInfo.getLevelShow();
			String[] levelShowArr = levelShow.split(",");

			// 将字符串数组转换为整数数组
			int[] intArray = Arrays.stream(levelShowArr)
					.mapToInt(Integer::parseInt)
					.toArray();

			// 判断是否符合购买条件
			if (!Arrays.stream(intArray).anyMatch(num -> num == memberBase.getLeaderLevel())) {
				throw new ServiceException("不符合购买条件", HttpStatus.ERROR);
			}


		} else {

			if (!lhLevelInfo.getLeaderLevel().equals(memberBase.getLeaderLevel())) {
				throw new ServiceException("不符合续费条件", HttpStatus.ERROR);
			}

			price = lhLevelInfo.getRenewPrice();
			// 续费如果需要积分，则处理积分逻辑
			if (lhLevelInfo.getRenewScore() > 0) {
				if (memberBase.getResidueScore() < lhLevelInfo.getRenewScore()) {
					throw new ServiceException("积分不足，不能进行续费", HttpStatus.ERROR);
				}

				// 减少积分
				lhMemberBaseService.reduceResidueScore(memberBase.getMemberId(), lhLevelInfo.getRenewScore());
			}
		}

		// 生成订单号
		String outTradeNo = wxPayUtil.getOutTradeNo();

		// 生产订单
		level.setOrderNumber(outTradeNo);
		level.setOrderType(level.getOrderType());
		level.setPurchaseType(lhLevelInfo.getPurchaseType());
		level.setTitle(lhLevelInfo.getTitle());
		level.setPrice(price);
		level.setPlatform(Constants.PLATFORM_3);
		level.setPayed(Constants.PAYED_NO);
		level.setMemberId(level.getMemberId());
		level.setCreateTime(new Date());
		lhOrdersLevelMapper.insert(level);

		/**
		 * 支付宝支付
		 */
		/**
		 * 金额转成字符串，0.01
		 */
		BigDecimal priceBigDecimal = new BigDecimal(price).setScale(2, RoundingMode.HALF_UP);
		String priceStr = priceBigDecimal.toString();
		String orderStr = "";

		try {
			orderStr = ZhiFuBaoUtil.pay(new ZhiFuBaoVo(outTradeNo,priceStr,lhLevelInfo.getTitle() + "购买"));
		} catch (AlipayApiException e) {
			throw new ServiceException("调用失败", HttpStatus.ERROR);
		}
		return orderStr;
	}

	/**
	 * VIP购买-积分方式
	 *
	 * @param level 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024年01月17日20:25:02
	 */
	@Override
	@Transactional
	public int insertOrdersLevelByScore(LhOrdersLevel level) {

		// 判断产品是否存在
		LhLevelInfo lhLevelInfo = lhLevelInfoService.getById(level.getLevelId());
		if (ObjectUtil.isNull(lhLevelInfo)) {
			throw new ServiceException("购买产品不存在", HttpStatus.ERROR);
		}

		// 判断用户是否已经购买
		LhMemberBase memberBase = lhMemberBaseService.getById(level.getMemberId());
		if (ObjectUtil.isNull(memberBase)) {
			throw new ServiceException("用户不存在", HttpStatus.ERROR);
		}


		if (memberBase.getResidueScore() < lhLevelInfo.getScore()) {
			throw new ServiceException("积分不足，不能进行积分购买", HttpStatus.ERROR);
		}

		String levelShow = lhLevelInfo.getLevelShow();
		String[] levelShowArr = levelShow.split(",");

		// 将字符串数组转换为整数数组
		int[] intArray = Arrays.stream(levelShowArr)
				.mapToInt(Integer::parseInt)
				.toArray();

		// 判断是否符合购买条件
		if (!Arrays.stream(intArray).anyMatch(num -> num == memberBase.getLeaderLevel())) {
			throw new ServiceException("不符合购买条件", HttpStatus.ERROR);
		}

		// 减少积分
		lhMemberBaseService.reduceResidueScore(memberBase.getMemberId(), lhLevelInfo.getScore());

		// 更新用户权益时间
		lhMemberBaseService.updateMemberLevelAndExpiryDate(memberBase.getMemberId(), lhLevelInfo.getLeaderLevel(), lhLevelInfo.getValidityDays(), Constants.ORDER_TYPE_1);

		// 生产订单
		level.setOrderNumber(wxPayUtil.getOutTradeNo());
		level.setOrderType(level.getOrderType());
		level.setPurchaseType(lhLevelInfo.getPurchaseType());
		level.setTitle(lhLevelInfo.getTitle());
		level.setPrice(lhLevelInfo.getPrice());
		level.setScore(lhLevelInfo.getScore());
		level.setPlatform(Constants.PLATFORM_4);
		level.setPayed(Constants.PAYED_YES);
		level.setPayedTime(new Date());
		level.setVipStartTime(new Date());

		// 获取当前日期
		LocalDate currentDate = LocalDate.now();

		// 增加增加天数
		LocalDate newDate = currentDate.plusDays(lhLevelInfo.getValidityDays());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = dateFormat.parse(newDate + " 23:59:59");
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		level.setVipEndTime(date);
		level.setMemberId(level.getMemberId());
		level.setCreateTime(new Date());

		return lhOrdersLevelMapper.insert(level);
	}

	/**
	 * 微信数据回调
	 *
	 * @param notifyData 对象
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	public void notifyOrdersLeve(String notifyData) {

		// 增加日志信息-返回日志
		Long orderLevelLogId = addOrdersLevelLog(notifyData);
		PrePayNotifyDataVo prePayNotifyDataVo = null;
		try {
			prePayNotifyDataVo = XmlUtils.convertXmlToObject(notifyData, PrePayNotifyDataVo.class);
		} catch (IOException e) {
			throw new ServiceException("数据转换异常" + e.getMessage(), HttpStatus.ERROR);
		}

		if (ObjectUtil.isNull(prePayNotifyDataVo)) {
			throw new ServiceException("数据处理异常", HttpStatus.ERROR);
		}

		// 查询订单
		LhOrdersLevel orderLevel = selectByOrderNumber(prePayNotifyDataVo.getOut_trade_no());
		if (ObjectUtil.isNull(orderLevel)) {
			updateOrdersLevelLog(orderLevelLogId, 2, "未查到订单信息");
			throw new ServiceException("未查到订单信息", HttpStatus.ERROR);
		}

		// 查询产品
		LhLevelInfo lhLevelInfo = lhLevelInfoService.getById(orderLevel.getLevelId());
		if (ObjectUtil.isNull(lhLevelInfo)) {
			updateOrdersLevelLog(orderLevelLogId, 2, "未查到产品信息");
			throw new ServiceException("未查到产品信息", HttpStatus.ERROR);
		}

		// 查询用户信息
		LhMemberBase lhMemberBase = lhMemberBaseService.getById(orderLevel.getMemberId());
		if (ObjectUtil.isNull(lhMemberBase)) {
			updateOrdersLevelLog(orderLevelLogId, 2, "未查到用户信息");
			throw new ServiceException("未查到用户信息", HttpStatus.ERROR);
		}
		// 判断是否支付
		if (Constants.PAYED_NO.equals(orderLevel.getPayed())) {

			// 判断金额是否相等
			if (!prePayNotifyDataVo.getTotal_fee().equals(String.valueOf(orderLevel.getPrice() * 100))) {
				updateOrdersLevelLog(orderLevelLogId, 2, "订单异常，金额不一致");
				throw new ServiceException("订单异常，金额不一致", HttpStatus.ERROR);
			}

			// 更新订单数据
			LhOrdersLevel lhOrdersLevel = new LhOrdersLevel();
			lhOrdersLevel.setOrderId(orderLevel.getOrderId());
			lhOrdersLevel.setPayed(Constants.PAYED_YES);
			lhOrdersLevel.setPayedTime(new Date());
			lhOrdersLevel.setUpdateTime(new Date());
			lhOrdersLevelMapper.updateById(lhOrdersLevel);

			// 判断订单类型
			if (orderLevel.getLevelId() != 7L ){   // 如果是自定义就不升级
				if (Constants.ORDER_TYPE_1.equals(orderLevel.getOrderType())) {

					//  判断是否升级
					if (!lhLevelInfo.getLeaderLevel().equals(lhMemberBase.getLeaderLevel())) {
						Integer numLevel = lhMemberBaseService.updateLeaderLevel(lhMemberBase.getMemberId(), lhLevelInfo.getLeaderLevel());
						if (numLevel <= 0) {
							updateOrdersLevelLog(orderLevelLogId, 2, "更新用户会员等级失败");
							throw new ServiceException("更新用户会员等级失败", HttpStatus.ERROR);
						}
					}
				}
			}


			// 更新用户权益时间
			lhMemberBaseService.updateMemberLevelAndExpiryDate(lhMemberBase.getMemberId(), lhLevelInfo.getLeaderLevel(), lhLevelInfo.getValidityDays(), orderLevel.getOrderType());

			// 更新用户消费记录
			LhMemberConsumedRec lhMemberConsumedRec = new LhMemberConsumedRec();
			lhMemberConsumedRec.setMemberId(lhMemberBase.getMemberId());
			lhMemberConsumedRec.setOrderNumber(orderLevel.getOrderNumber());
			lhMemberConsumedRec.setAmount(new BigDecimal(orderLevel.getPrice()));
			memberConsumedRecService.insertMemberConsumedRec(lhMemberConsumedRec);

			// 更新日志
			updateOrdersLevelLog(orderLevelLogId, 1, "成功");
		} else {
			updateOrdersLevelLog(orderLevelLogId, 2, "订单异常，订单不可重复支付");
		}
	}

	/**
	 * 支付宝数据回调
	 *
	 * @param notifyData 对象
	 * @author tianliang
	 * @date 2024/1/21
	 */
	// TODO 支付宝回调待完善未测试
	@Override
	public void notifyZhiFuBaoOrdersLeve(ZhiFuBaoPayNotifyDataVo notifyData) {
		// 先判断当下的支付宝的交易号存不存在
		LambdaQueryWrapper<LhOrdersLevelLog> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhOrdersLevelLog::getReason,notifyData.getTrade_no());
		LhOrdersLevelLog ordersLevelLog = ordersLevelLogService.getOne(queryWrapper);

		if ("WAIT_BUYER_PAY".equals(notifyData.getTrade_status())){
			// 交易创建
			// 增加日志信息-返回日志
			if (ObjectUtil.isNull(ordersLevelLog)){
				addZhiFuBaoOrdersLevelLog(notifyData);
			}
		}else if ("TRADE_SUCCESS".equals(notifyData.getTrade_status())){
			// 交易支付成功
			// 日志记录
			Long orderLevelLogId = ordersLevelLog.getOrderId();

			// 查询订单
			LhOrdersLevel orderLevel = selectByOrderNumber(notifyData.getOut_trade_no());
			if (ObjectUtil.isNull(orderLevel)) {
				updateOrdersLevelLog(orderLevelLogId, 2, "未查到订单信息");
				throw new ServiceException("未查到订单信息", HttpStatus.ERROR);
			}

			// 查询产品
			LhLevelInfo lhLevelInfo = lhLevelInfoService.getById(orderLevel.getLevelId());
			if (ObjectUtil.isNull(lhLevelInfo)) {
				updateOrdersLevelLog(orderLevelLogId, 2, "未查到产品信息");
				throw new ServiceException("未查到产品信息", HttpStatus.ERROR);
			}

			// 查询用户信息
			LhMemberBase lhMemberBase = lhMemberBaseService.getById(orderLevel.getMemberId());
			if (ObjectUtil.isNull(lhMemberBase)) {
				updateOrdersLevelLog(orderLevelLogId, 2, "未查到用户信息");
				throw new ServiceException("未查到用户信息", HttpStatus.ERROR);
			}


			// 判断是否支付
			if (Constants.PAYED_NO.equals(orderLevel.getPayed())) {

				// 判断金额是否相等
				Float total_amount = notifyData.getReceipt_amount();
				Float price = (float)orderLevel.getPrice();
				if (total_amount - price != 0) {
					updateOrdersLevelLog(orderLevelLogId, 2, "订单异常，金额不一致");
					throw new ServiceException("订单异常，金额不一致", HttpStatus.ERROR);
				}

				// 更新订单数据
				LhOrdersLevel lhOrdersLevel = new LhOrdersLevel();
				lhOrdersLevel.setOrderId(orderLevel.getOrderId());
				lhOrdersLevel.setPayed(Constants.PAYED_YES);
				lhOrdersLevel.setPayedTime(new Date());
				lhOrdersLevel.setUpdateTime(new Date());
				lhOrdersLevelMapper.updateById(lhOrdersLevel);

				// 判断订单类型
				if (Constants.ORDER_TYPE_1.equals(orderLevel.getOrderType())) {

					//  判断是否升级
					if (!lhLevelInfo.getLeaderLevel().equals(lhMemberBase.getLeaderLevel())) {
						Integer numLevel = lhMemberBaseService.updateLeaderLevel(lhMemberBase.getMemberId(), lhLevelInfo.getLeaderLevel());
						if (numLevel <= 0) {
							updateOrdersLevelLog(orderLevelLogId, 2, "更新用户会员等级失败");
							throw new ServiceException("更新用户会员等级失败", HttpStatus.ERROR);
						}
					}
				}

				// 更新用户权益时间
				lhMemberBaseService.updateMemberLevelAndExpiryDate(lhMemberBase.getMemberId(), lhLevelInfo.getLeaderLevel(), lhLevelInfo.getValidityDays(), orderLevel.getOrderType());

				// 更新日志
				updateOrdersLevelLog(orderLevelLogId, 1, "成功");
			} else {
				updateOrdersLevelLog(orderLevelLogId, 2, "订单异常，订单不可重复支付");
			}

		}


	}



	/**
	 * 新增支付宝的日志信息
	 *
	 * @param notifyData 返回数据
	 * @author zhanggl
	 * @date 2024年01月05日19:57:13
	 */
	public Long addZhiFuBaoOrdersLevelLog(ZhiFuBaoPayNotifyDataVo notifyData) {



		LhOrdersLevelLog ordersLevelLog = new LhOrdersLevelLog();
		if (ObjectUtil.isNotNull(notifyData.getOut_trade_no())) {
			ordersLevelLog.setOrderNumber(notifyData.getOut_trade_no());
		}
		ordersLevelLog.setFromTxt(notifyData.toString());
		ordersLevelLog.setStatus(Constants.GeneralIntType.ZERO);
		// 存放的是 支付宝交易号
		ordersLevelLog.setReason(notifyData.getTrade_no());

		// 增加回调日志记录
		return ordersLevelLogService.insertOrdersLevelLog(ordersLevelLog);
	}

	/**
	 * 新增日志信息
	 *
	 * @param notifyData 返回数据
	 * @author zhanggl
	 * @date 2024年01月05日19:57:13
	 */
	public Long addOrdersLevelLog(String notifyData) {

		PrePayNotifyDataVo prePayNotifyDataVo = null;
		try {
			prePayNotifyDataVo = XmlUtils.convertXmlToObject(notifyData, PrePayNotifyDataVo.class);
		} catch (IOException e) {
			throw new ServiceException("转换格式失败" + e.getMessage(), HttpStatus.ERROR);
		}

		LhOrdersLevelLog ordersLevelLog = new LhOrdersLevelLog();
		if (ObjectUtil.isNotNull(prePayNotifyDataVo)) {
			ordersLevelLog.setOrderNumber(prePayNotifyDataVo.getOut_trade_no());
		}
		ordersLevelLog.setFromTxt(notifyData);
		ordersLevelLog.setStatus(Constants.GeneralIntType.ZERO);
		ordersLevelLog.setReason("待处理");

		// 增加回调日志记录
		return ordersLevelLogService.insertOrdersLevelLog(ordersLevelLog);
	}

	/**
	 * 修改日志信息
	 *
	 * @param orderLevelLogId 日志ID
	 * @param status          状态
	 * @param reason          备注
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	private void updateOrdersLevelLog(Long orderLevelLogId, Integer status, String reason) {

		LhOrdersLevelLog ordersLevelLog = new LhOrdersLevelLog();
		ordersLevelLog.setOrderId(orderLevelLogId);
		ordersLevelLog.setStatus(status);
		ordersLevelLog.setReason(reason);
		ordersLevelLogService.updateOrdersLevelLog(ordersLevelLog);
	}

	/**
	 * 根据订单编号查询订单
	 *
	 * @param orderNumber 订单编号
	 * @return com.lanhe.order.domain.LhOrdersLevel
	 * @author zhanggl
	 * @date 2024/1/5
	 */
	@Override
	public LhOrdersLevel selectByOrderNumber(String orderNumber) {
		LambdaQueryWrapper<LhOrdersLevel> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhOrdersLevel::getOrderNumber, orderNumber);
		return lhOrdersLevelMapper.selectOne(queryWrapper);
	}

	@Override
	public List<LhOrdersLevel> selectLhOrdersLevelList(LhOrdersLevel lhOrdersLevel) {
		return lhOrdersLevelMapper.selectLhOrdersLevelList(lhOrdersLevel);
	}

}


